;
const toHex = (n) => `${n > 15 ? "" : 0}${n.toString(16)}`;
const toHexString = (colorObj) => {
  const { r, g, b, a = 1 } = colorObj;
  return `#${toHex(r)}${toHex(g)}${toHex(b)}${a === 1 ? "" : toHex(Math.floor(a * 255))}`;
};
const toRgbString = (colorObj) => {
  const { r, g, b } = colorObj;
  return `rgb(${r},${g},${b})`;
};
const toRgbaString = (colorObj, n = 1e4) => {
  const { r, g, b, a = 1 } = colorObj;
  return `rgba(${r},${g},${b},${Math.floor(a * n) / n})`;
};
const parseHexColor = (color) => {
  let hex = color.slice(1);
  let a = 1;
  if (hex.length === 3) {
    hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`;
  }
  if (hex.length === 8) {
    a = parseInt(hex.slice(6), 16) / 255;
    hex = hex.slice(0, 6);
  }
  const bigint = parseInt(hex, 16);
  return {
    r: bigint >> 16 & 255,
    g: bigint >> 8 & 255,
    b: bigint & 255,
    a
  };
};
const parseRgbaColor = (color) => {
  const arr = color.match(/(\d(\.\d+)?)+/g) || [];
  const res = arr.map((s) => parseInt(s, 10));
  return {
    r: res[0],
    g: res[1],
    b: res[2],
    a: arr[3] === void 0 ? 1 : parseFloat(arr[3])
  };
};
const parseColorString = (color) => {
  if (color.startsWith("#")) {
    return parseHexColor(color);
  } else if (color.startsWith("rgb")) {
    return parseRgbaColor(color);
  } else if (color === "transparent") {
    return parseHexColor("#ffffff00");
  }
  throw new Error(`color string error: ${color}`);
};
const getColorInfo = (color) => {
  if (!color) {
    return {
      rgbaObj: {
        r: 255,
        g: 0,
        b: 0,
        a: 1
      },
      hex: "",
      rgba: "",
      rgb: ""
    };
  }
  const colorObj = parseColorString(color);
  const hex = toHexString(colorObj);
  const rgba = toRgbaString(colorObj);
  const rgb = toRgbString(colorObj);
  return {
    hex,
    rgba,
    rgb,
    rgbaObj: colorObj
  };
};
const hexToRgba = (hex) => {
  const colorObj = parseColorString(hex);
  return toRgbaString(colorObj);
};
const rgbaToHex = (rgba) => {
  const colorObj = parseColorString(rgba);
  return toHexString(colorObj);
};
const hsvToRgb = (h, s, v) => {
  h === 360 && (h = 0);
  let i = Math.floor(h / 60) % 6;
  let f = h / 60 - i;
  let p = v * (1 - s);
  let q = v * (1 - s * f);
  let t = v * (1 - s * (1 - f));
  let r = 0;
  let g = 0;
  let b = 0;
  if (i === 0) {
    r = v;
    g = t;
    b = p;
  } else if (i === 1) {
    r = q;
    g = v;
    b = p;
  } else if (i === 2) {
    r = p;
    g = v;
    b = t;
  } else if (i === 3) {
    r = p;
    g = q;
    b = v;
  } else if (i === 4) {
    r = t;
    g = p;
    b = v;
  } else if (i === 5) {
    r = v;
    g = p;
    b = q;
  }
  r = Math.floor(r * 255 + 0.5);
  g = Math.floor(g * 255 + 0.5);
  b = Math.floor(b * 255 + 0.5);
  return { r, g, b };
};
const rgbToHsv = (r, g, b) => {
  let r1 = r / 255;
  let g1 = g / 255;
  let b1 = b / 255;
  let cmax = Math.max(r1, g1, b1);
  let cmin = Math.min(r1, g1, b1);
  let d = cmax - cmin;
  let h = 0;
  let s = 1;
  let v = 1;
  if (d === 0) {
    h = 0;
  } else if (cmax === r1) {
    h = (60 * (g1 - b1) / d + 360) % 360;
  } else if (cmax === g1) {
    h = 60 * ((b1 - r1) / d + 2);
  } else if (cmax === b1) {
    h = 60 * ((r1 - g1) / d + 4);
  }
  if (cmax === 0) {
    s = 0;
  } else {
    s = d / cmax;
  }
  v = cmax;
  h = Math.floor(h + 0.5);
  s = Math.floor(s * 100 + 0.5) / 100;
  v = Math.floor(v * 100 + 0.5) / 100;
  return { h, s, v };
};
export {
  getColorInfo,
  hexToRgba,
  hsvToRgb,
  parseColorString,
  parseHexColor,
  parseRgbaColor,
  rgbToHsv,
  rgbaToHex,
  toHex,
  toHexString,
  toRgbString,
  toRgbaString
};
//# sourceMappingURL=color.mjs.map
